package br.com.jvet.persistencia;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.Query;

import br.com.jvet.entidade.Raca;
import br.com.jvet.entidade.TipoAnimal;

@Stateless
public class RacaDAO extends AbstractDAO<Raca, Integer> {

	private static String SELECT_BUSCA = "SELECT t from "
			+ Raca.class.getSimpleName()
			+ " t where upper(t.descricao) =:descricao and t.tipoAnimal =:tipoanimal";

	public RacaDAO() {
		super(Raca.class);

	}

	public boolean existe(Raca raca) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("descricao", raca.getDescricao().toUpperCase().trim());
		parameters.put("tipoanimal", raca.getTipoAnimal());
		Raca outro = super.buscarUmResultado(SELECT_BUSCA, parameters);
		if (outro != null && outro.getCodigo() != raca.getCodigo()) {
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<Raca> buscaPorTipoAnimal(TipoAnimal tipo) {
		Query c = super.getEntityManager().createQuery(
				"SELECT r FROM " + Raca.class.getSimpleName()
						+ " r  where r.tipoAnimal =:tipo");

		c.setParameter("tipo", tipo);

		return (List<Raca>) c.getResultList();
	}

}
